package com.egdd.video.exception;

import com.egdd.video.common.api.ApiResult;
import com.egdd.video.common.api.Constants;
import com.egdd.video.common.api.HttpStatus;
import com.egdd.video.exception.file.FileSizeLimitExceededException;
import com.egdd.video.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.security.auth.login.AccountExpiredException;
import java.io.IOException;

/**
 * 全局异常处理器
 *
 * @author api
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 基础异常
     */
    @ExceptionHandler(BaseException.class)
    public ApiResult baseException(BaseException e) {

        return ApiResult.error(e.getMessage());
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public ApiResult businessException(CustomException e) {
        if (StringUtils.isNull(e.getCode())) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ApiResult handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return ApiResult.error(HttpStatus.NOT_FOUND, "路径不存在，请检查路径是否正确");
    }

  /*  @ExceptionHandler(AccessDeniedException.class)
    public ApiResult handleAuthorizationException(AccessDeniedException e) {
        log.error(e.getMessage());
        return ApiResult.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
    }*/

    @ExceptionHandler(AccountExpiredException.class)
    public ApiResult handleAccountExpiredException(AccountExpiredException e) {
        log.error(e.getMessage(), e);
        return ApiResult.error(e.getMessage());
    }

   /* @ExceptionHandler(UsernameNotFoundException.class)
    public ApiResult handleUsernameNotFoundException(UsernameNotFoundException e) {
        log.error(e.getMessage(), e);
        return ApiResult.error(e.getMessage());
    }*/

    @ExceptionHandler(Exception.class)
    public ApiResult handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ApiResult.error(e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public ApiResult validatedBindException(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return ApiResult.error(message);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ApiResult uploadException(MaxUploadSizeExceededException e) {
        log.error(e.getMessage());
        return ApiResult.error(Constants.UPLOAD_MAXSIZE);
    }


}
